package org.thinker.sp1;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.thinker.bbs.BbsVO;

@Controller
@RequestMapping("/form")

public class FormController {

	static Logger logger = Logger.getLogger(FormController.class);
	
	
	@RequestMapping( method=RequestMethod.GET)
	public String viewForm(Model model){
		logger.info("viewForm");

		model.addAttribute(new BbsVO());
		
		return "form/create";
		
	}
	
	
	
	@RequestMapping(method=RequestMethod.POST)
	public String processSubmit(@Valid BbsVO vo, BindingResult result, Model model, RedirectAttributes redirectAttrs){
		
			
		if (result.hasErrors()) {
			logger.info(result.toString());
			model.addAttribute(vo);
			return "form/create";			
		}		

		logger.info(vo);
		
		return "success";		
	}
	
}
